package com.henu.acmrating.controller;

import com.alibaba.fastjson.JSONObject;
import com.henu.acmrating.common.Result;
import com.henu.acmrating.pojo.Blog;
import com.henu.acmrating.pojo.BlogLike;
import com.henu.acmrating.pojo.BlogWithUser;
import com.henu.acmrating.service.BlogLikeService;
import com.henu.acmrating.service.BlogService;
import com.henu.acmrating.utils.JwtUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api")
public class BlogController {
    @Resource
    BlogService blogService;

    @Resource
    BlogLikeService blogLikeService;

    /**
     * 拿到所有博客
     */
    @RequestMapping(value = "/getAllBlog", method = RequestMethod.POST)
    public Result getAllBlog(HttpServletRequest request) {
        // 1.查询公开的博客并带上用户
        List<BlogWithUser> blogList = blogService.blogWithUser();

        // 2.查询当前用户的喜欢博客列表
        int userId = JwtUtils.getUserId(request);
        List<Integer> likeList = blogLikeService.selectBlogIdByUserIdAndIsLike(userId);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("blogList", blogList);
        jsonObject.put("likeList", likeList);

        return Result.success(jsonObject);
    }

    @RequestMapping(value = "/updateLike", method = RequestMethod.POST)
    public Result updateLike(@RequestBody BlogLike blogLike, HttpServletRequest request) {
        // 1.查询当前用户id并设置userId
        int userId = JwtUtils.getUserId(request);
        blogLike.setUserId(userId);


        // 2. 根据blogId和userId查找id更新喜欢状态
        BlogLike blogLikeDB = blogLikeService.selectOneByUserIdAndBlogId(blogLike);
        if(blogLikeDB != null) {
            blogLike.setId(blogLikeDB.getId());
        }
        blogLike.setIsLike(blogLike.getIsLike() == 1 ? 0 : 1);
        blogLikeService.insertOrUpdate(blogLike);

        // 3. 更新博客喜欢次数
        int count = blogLike.getIsLike() == 1 ? 1 : -1;
        blogService.updateLikeById(count, blogLike.getBlogId());

        return Result.success();
    }

    @RequestMapping(value = "/detailBlog", method = RequestMethod.GET)
    public Result detailBlog(@RequestParam("id") Integer blogId,
                             HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        int userId = JwtUtils.getUserId(request);
        // 1.根据博客id 查询
        BlogWithUser blogWithUser = blogService.blogWithUserOne(userId, blogId);
        if(blogWithUser == null) {
            // TODO 统一地址错误
        } else {
            jsonObject.put("blog", blogWithUser);
        }
        // 2. 根据用户id和博客id 判断是否喜欢
        BlogLike blogLike = new BlogLike();
        blogLike.setBlogId(blogId);
        blogLike.setUserId(userId);
        BlogLike blogLikeDB = blogLikeService.selectOneByAll(blogLike);
        jsonObject.put("isLike", blogLikeDB == null ? 0 : blogLikeDB.getIsLike());
        return Result.success(jsonObject);
    }

    /**
     * 我的博客 基本和主页一样只是加一个userId
     */
    @RequestMapping(value = "/myBlog")
    public Result myBlog(HttpServletRequest request) {
        int userId = JwtUtils.getUserId(request);
        List<BlogWithUser> blogList = blogService.blogWithUserByUserId(userId);
        List<Integer> likeList = blogLikeService.selectBlogIdByUserIdAndIsLike(userId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("blogList", blogList);
        jsonObject.put("likeList", likeList);
        return Result.success(jsonObject);
    }

    /**
     * 新建博客或者更新博客
     */
    @RequestMapping(value = "newBlog", method = RequestMethod.POST)
    public Result newBlog(@RequestBody Blog blog, HttpServletRequest request) {
        // 1.先判断有没有传过来id 有id更新，否插入
        int userId = JwtUtils.getUserId(request);
        String msg = "";
        if(blog.getId() != null) {
            // 2.更新博客 先判断是不是本用户更新
            if(blog.getUserId() == userId) {
                blog.setUpdateDate(JwtUtils.getDate());
                blogService.updateByPrimaryKey(blog);
                msg = "更新成功";
            }
        } else {
            // 3.新建博客
            blog.setCreateDate(JwtUtils.getDate());
            blog.setUpdateDate(blog.getCreateDate());
            blog.setLike(0);
            blog.setUserId(userId);
            blogService.insert(blog);
            msg = "发布成功";
        }
        if(msg == "") {
            return Result.error("异常错误");
        }
        return Result.success(msg);
    }
}

